package cn.edu.zafu.bigdata.x.wupeng.h;

import java.io.*;
import java.util.ArrayList;

class Goods implements Serializable {
    private String desc;
    private transient double price;
    private int unit;

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getUnit() {
        return unit;
    }

    public void setUnit(int unit) {
        this.unit = unit;
    }

    public Goods(String desc, double price, int unit) {
        this.desc = desc;
        this.price = price;
        this.unit = unit;
    }
}

public class SerializeDemo {
    public static void main(String[] args) throws IOException {

        ArrayList<Goods> list = new ArrayList<>();
        Goods java = new Goods("Java", 99.9, 100);
        Goods python = new Goods("Python", 98.9, 100);
        Goods php = new Goods("PHP", 38.9, 10);
        for (int i = 0; i < 10000; i++) {
            list.add(java);
            list.add(python);
            list.add(php);
        }

        FileOutputStream fos = new FileOutputStream("files/goods.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(list);
        fos.close();

        FileInputStream fis = new FileInputStream("files/goods.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);
        try {
            ArrayList<Goods> newGoods = (ArrayList<Goods>) ois.readObject();
            for (Goods goods : newGoods){
                System.out.println(goods.getDesc() + " " + goods.getPrice() + " " + goods.getUnit());
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        fis.close();
    }
}
